//
//  UITableViewEx.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/7/24.
//

import Foundation

extension UITableView {
    /// 注册UITableViewCell
    /// - Parameter cellClass: cell的类型
    func wmc_register(cellClass: UITableViewCell.Type) {
        let reuseId = Self.identifier(for: cellClass)
        register(cellClass, forCellReuseIdentifier: reuseId)
    }
    
    /// 批量注册UITableViewCell
    /// - Parameter cellClasses: cell类型数组
    func wmc_register(cellClasses: [UITableViewCell.Type]) {
        cellClasses.forEach { cellClass in
            wmc_register(cellClass: cellClass)
        }
    }
    
    /// 获取UITableViewCell
    func wmc_dequeueReusableCell<T:UITableViewCell>(cellClass: T.Type, for indexPath: IndexPath) -> T {
        let reuseId = Self.identifier(for: cellClass)
        let cell = dequeueReusableCell(withIdentifier: reuseId, for: indexPath) as! T
        return cell
    }
    /// 注册UITableViewHeaderFooterView
    func wmc_register(headerFooterClass: UITableViewHeaderFooterView.Type) {
        let reuseId = Self.identifier(for: headerFooterClass)
        register(headerFooterClass, forHeaderFooterViewReuseIdentifier: reuseId)
    }
    
    /// 获取UITableViewHeaderFooterView
    func wmc_dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>(headerFooterClass: T.Type) -> T {
        let reuseId = Self.identifier(for: headerFooterClass)
        return dequeueReusableHeaderFooterView(withIdentifier: reuseId) as! T
    }
    
    /// 根据Cell类型返回Cell重用标识符
    private static func identifier(for cellClass: UITableViewCell.Type) -> String {
        let className = String(describing: cellClass)
        return className
    }
    
    /// 根据HeaderFooter类型返回重用标识
    private static func identifier(for headerFooterClass: UITableViewHeaderFooterView.Type) -> String {
        let className = String(describing: headerFooterClass)
        return className
    }
}
